#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:pengfeifu
@file: mzitu_win.py.py 
@version:
@time: 2021/11/06 
@email:1324734112@qq.com
@desc： 爬取妹子图网站图片
@function：常用函数
设计规则：
1.获取网站最外层分页，拿到最大页码
2.遍历每一页，并根据每一页，设置文件夹
3.获取每一张页面的，每一张图片的a标签路径
4.请求a路径
5.获取每个页面的最大分页
6.拼接每一页的路径，再请求路径
7.保存每一页的图片
"""

import os
import random
import requests
import bs4
from bs4 import BeautifulSoup
import sys
import time
import importlib

importlib.reload(sys)

# user_agent用户代理，用来匹配各个操作系统
meizi_headers = [
    "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36",
    "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/537.75.14",
    "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; Trident/6.0)",
    'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11',
    'Opera/9.25 (Windows NT 5.1; U; en)',
    'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
    'Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko) (Kubuntu)',
    'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12',
    'Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/1.2.9',
    "Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.7 (KHTML, like Gecko) Ubuntu/11.04 Chromium/16.0.912.77 Chrome/16.0.912.77 Safari/535.7",
    "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:10.0) Gecko/20100101 Firefox/10.0",
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36'
]

# 给定统一的请求头来模拟Chrome浏览器，global类似static,全局变量，如果不指定，在方法里可能会被认为是局部变量
global headers
headers = {"User-Agent": random.choice(meizi_headers)}

global file_save_path
file_save_path = "D:/net/picture/mzitu"

# 爬虫地址
mzitu_url = "https://www.mzitu.com/"


# 创建文件夹
def mkdir_directory(file_path):
    if os.path.exists(file_path) is False:
        # 创建文件夹
        os.makedirs(file_path)
    # 切换到当前路径
    os.chdir(file_path)


# 文件写入
def pic_write(file_name, content):
    try:
        with open(file_name, "ab") as f:
            f.write(content)
    except IOError:
        print("文件写入失败")
    finally:
        f.close()


def pic_download(page_url):
    # page_url一级页面地址
    sub_res = requests.get(page_url, headers=headers)
    sub_soup = BeautifulSoup(sub_res.text, "html.parser")
    # 获取当前页面，每个封面的a标签地址
    a_target_list = sub_soup.find("div", class_="postlist").find("span").find_all("a", target='_blank')
    for a in a_target_list:
        href = a.attrs["href"]  # 获取a标签的href属性
        inner_sub_res = requests.get(href, headers=headers)  # 二级页面的内部请求响应
        inner_sub_soup = BeautifulSoup(inner_sub_res.text, "html.parser")
        # 获取二级页面的页码
        page_total = inner_sub_soup.find("div", class_="pagenavi").find_all("span")[6].text
        for i in range(1, int(page_total) + 1):
            time.sleep(random.randint(1, 2))  # 随机休眠1~2秒
            # 获取每页的地址栏路径
            inner_page_url = href + "/" + str(i)
            print("*" * 20, "正在读取二级页面地址：", inner_page_url, "*" * 20)
            sub_res2 = requests.get(inner_page_url, headers=headers)
            sub_soup2 = BeautifulSoup(sub_res2.text, "html.parser")
            img_tag = sub_soup2.find("div", class_="main-image").find("img")
            # 判断img_tag是否是tag标签
            if isinstance(img_tag, bs4.element.Tag):
                img_src = img_tag.attrs["src"]
                # 切割图片路径，获取文件名
                array = img_src.split("/")
                img_name = array[len(array) - 1]
                # 防盗链加入Referer
                header = {"User-Agent": random.choice(meizi_headers), "Referer": img_src}
                img = requests.get(img_src, headers=header)
                print("开始保存图片：", img_src)
                pic_write(img_name, img.content)


if __name__ == "__main__":
    # 获取首页信息
    res = requests.get(mzitu_url, headers=headers)
    # 使用自带的html.parser解析标签
    soup = BeautifulSoup(res.text, "html.parser")
    # 获取页面总页码
    page_num = soup.find("div", class_='nav-links').find_all("a")[3].text
    # 遍历页码
    for i in range(1, int(page_num) + 1):
        # 创建文件夹
        mkdir_directory(file_save_path + "\\第%s页" % i)
        # 获取每页地址栏地址
        if i == 1:
            page = mzitu_url
        else:
            page = mzitu_url + "page/" + str(i)
        print("第%s页套图地址：%s" % (i, page))
        pic_download(page)
